package com.czc.weather.push.weather.utils;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.ChineseDate;

//日期相关工具类
public class DateUtils {


    public static String calculateBirthday(String birthDateStr) {
        // 解析出生日期为 Date 对象
        Date birthDate = DateUtil.parse(birthDateStr);

        // 获取当前日期
        Date now = new Date();

        // 将出生日期转换为农历
        ChineseDate chineseBirthDate = new ChineseDate(birthDate);
        int lunarMonth = chineseBirthDate.getMonth();
        int lunarDay = chineseBirthDate.getDay();


        // 计算今年和明年的农历生日对应的阳历日期
        Date thisYearBirthday = calculateLunarBirthday(lunarMonth, lunarDay, DateUtil.year(now));
        Date nextYearBirthday = calculateLunarBirthday(lunarMonth, lunarDay, DateUtil.year(now) + 1);

        // 判断今年的生日是否已经过去
        if (DateUtil.compare(now, thisYearBirthday) > 0) {
            // 今年的生日已经过去，计算到明年的生日
            long daysUntilBirthday = DateUtil.betweenDay(now, nextYearBirthday, true);
            return String.valueOf(daysUntilBirthday);
        } else {
            // 今年的生日还没到，计算到今年的生日
            long daysUntilBirthday = DateUtil.betweenDay(now, thisYearBirthday, true);
            return String.valueOf(daysUntilBirthday);
        }
    }

    /**
     * 根据农历月份和日期，计算对应的阳历日期
     *
     * @param lunarMonth 农历月份
     * @param lunarDay   农历日期
     * @param year       阳历年份
     * @return 对应的阳历日期
     */
    private static Date calculateLunarBirthday(int lunarMonth, int lunarDay, int year) {
        // 使用 Hutool 的 ChineseDate 构造农历日期
        ChineseDate chineseDate = new ChineseDate(year, lunarMonth, lunarDay);
        // 转换为阳历日期
        return chineseDate.getGregorianDate();
    }


        public static String calculateDaysDifference(String inputDateString) {
        // 定义日期格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        // 解析传入的日期字符串
        LocalDate inputDate = LocalDate.parse(inputDateString, formatter);

        // 获取当前日期
        LocalDate today = LocalDate.now();

        // 计算两个日期之间的天数差
        long daysBetween = ChronoUnit.DAYS.between(inputDate, today);

        // 强制类型转换为int（假设差值不会超过Integer.MAX_VALUE）
        return String.valueOf(daysBetween);
    }
}
